/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.entities.PriceSlaughterLamb;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import times.persistence.controllers.exceptions.IllegalOrphanException;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.PriceSlaughter;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.SlaughterProperty;

/**
 *
 * @author Administrator
 */
public class PriceSourceSlaughterHeaderJpaController implements Serializable {

    public PriceSourceSlaughterHeaderJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PriceSourceSlaughterHeader priceSourceSlaughterHeader) {
        if (priceSourceSlaughterHeader.getPriceSlaughterLambList() == null) {
            priceSourceSlaughterHeader.setPriceSlaughterLambList(new ArrayList<PriceSlaughterLamb>());
        }
        if (priceSourceSlaughterHeader.getPriceSlaughterList() == null) {
            priceSourceSlaughterHeader.setPriceSlaughterList(new ArrayList<PriceSlaughter>());
        }
        if (priceSourceSlaughterHeader.getSlaughterPropertyList() == null) {
            priceSourceSlaughterHeader.setSlaughterPropertyList(new ArrayList<SlaughterProperty>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<PriceSlaughterLamb> attachedPriceSlaughterLambList = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLambToAttach : priceSourceSlaughterHeader.getPriceSlaughterLambList()) {
                priceSlaughterLambListPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambList.add(priceSlaughterLambListPriceSlaughterLambToAttach);
            }
            priceSourceSlaughterHeader.setPriceSlaughterLambList(attachedPriceSlaughterLambList);
            List<PriceSlaughter> attachedPriceSlaughterList = new ArrayList<PriceSlaughter>();
            for (PriceSlaughter priceSlaughterListPriceSlaughterToAttach : priceSourceSlaughterHeader.getPriceSlaughterList()) {
                priceSlaughterListPriceSlaughterToAttach = em.getReference(priceSlaughterListPriceSlaughterToAttach.getClass(), priceSlaughterListPriceSlaughterToAttach.getId());
                attachedPriceSlaughterList.add(priceSlaughterListPriceSlaughterToAttach);
            }
            priceSourceSlaughterHeader.setPriceSlaughterList(attachedPriceSlaughterList);
            List<SlaughterProperty> attachedSlaughterPropertyList = new ArrayList<SlaughterProperty>();
            for (SlaughterProperty slaughterPropertyListSlaughterPropertyToAttach : priceSourceSlaughterHeader.getSlaughterPropertyList()) {
                slaughterPropertyListSlaughterPropertyToAttach = em.getReference(slaughterPropertyListSlaughterPropertyToAttach.getClass(), slaughterPropertyListSlaughterPropertyToAttach.getId());
                attachedSlaughterPropertyList.add(slaughterPropertyListSlaughterPropertyToAttach);
            }
            priceSourceSlaughterHeader.setSlaughterPropertyList(attachedSlaughterPropertyList);
            em.persist(priceSourceSlaughterHeader);
            for (PriceSlaughterLamb priceSlaughterLambListPriceSlaughterLamb : priceSourceSlaughterHeader.getPriceSlaughterLambList()) {
                PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListPriceSlaughterLamb = priceSlaughterLambListPriceSlaughterLamb.getPriceSourceSlaughterHeaderId();
                priceSlaughterLambListPriceSlaughterLamb.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                priceSlaughterLambListPriceSlaughterLamb = em.merge(priceSlaughterLambListPriceSlaughterLamb);
                if (oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListPriceSlaughterLamb != null) {
                    oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListPriceSlaughterLamb);
                    oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListPriceSlaughterLamb = em.merge(oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListPriceSlaughterLamb);
                }
            }
            for (PriceSlaughter priceSlaughterListPriceSlaughter : priceSourceSlaughterHeader.getPriceSlaughterList()) {
                PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListPriceSlaughter = priceSlaughterListPriceSlaughter.getPriceSourceSlaughterHeaderId();
                priceSlaughterListPriceSlaughter.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                priceSlaughterListPriceSlaughter = em.merge(priceSlaughterListPriceSlaughter);
                if (oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListPriceSlaughter != null) {
                    oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListPriceSlaughter.getPriceSlaughterList().remove(priceSlaughterListPriceSlaughter);
                    oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListPriceSlaughter = em.merge(oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListPriceSlaughter);
                }
            }
            for (SlaughterProperty slaughterPropertyListSlaughterProperty : priceSourceSlaughterHeader.getSlaughterPropertyList()) {
                PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListSlaughterProperty = slaughterPropertyListSlaughterProperty.getPriceSourceSlaughterHeaderId();
                slaughterPropertyListSlaughterProperty.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                slaughterPropertyListSlaughterProperty = em.merge(slaughterPropertyListSlaughterProperty);
                if (oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListSlaughterProperty != null) {
                    oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListSlaughterProperty.getSlaughterPropertyList().remove(slaughterPropertyListSlaughterProperty);
                    oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListSlaughterProperty = em.merge(oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListSlaughterProperty);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PriceSourceSlaughterHeader priceSourceSlaughterHeader) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSourceSlaughterHeader persistentPriceSourceSlaughterHeader = em.find(PriceSourceSlaughterHeader.class, priceSourceSlaughterHeader.getId());
            List<PriceSlaughterLamb> priceSlaughterLambListOld = persistentPriceSourceSlaughterHeader.getPriceSlaughterLambList();
            List<PriceSlaughterLamb> priceSlaughterLambListNew = priceSourceSlaughterHeader.getPriceSlaughterLambList();
            List<PriceSlaughter> priceSlaughterListOld = persistentPriceSourceSlaughterHeader.getPriceSlaughterList();
            List<PriceSlaughter> priceSlaughterListNew = priceSourceSlaughterHeader.getPriceSlaughterList();
            List<SlaughterProperty> slaughterPropertyListOld = persistentPriceSourceSlaughterHeader.getSlaughterPropertyList();
            List<SlaughterProperty> slaughterPropertyListNew = priceSourceSlaughterHeader.getSlaughterPropertyList();
            List<String> illegalOrphanMessages = null;
            for (PriceSlaughterLamb priceSlaughterLambListOldPriceSlaughterLamb : priceSlaughterLambListOld) {
                if (!priceSlaughterLambListNew.contains(priceSlaughterLambListOldPriceSlaughterLamb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PriceSlaughterLamb " + priceSlaughterLambListOldPriceSlaughterLamb + " since its priceSourceSlaughterHeaderId field is not nullable.");
                }
            }
            for (PriceSlaughter priceSlaughterListOldPriceSlaughter : priceSlaughterListOld) {
                if (!priceSlaughterListNew.contains(priceSlaughterListOldPriceSlaughter)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PriceSlaughter " + priceSlaughterListOldPriceSlaughter + " since its priceSourceSlaughterHeaderId field is not nullable.");
                }
            }
            for (SlaughterProperty slaughterPropertyListOldSlaughterProperty : slaughterPropertyListOld) {
                if (!slaughterPropertyListNew.contains(slaughterPropertyListOldSlaughterProperty)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SlaughterProperty " + slaughterPropertyListOldSlaughterProperty + " since its priceSourceSlaughterHeaderId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<PriceSlaughterLamb> attachedPriceSlaughterLambListNew = new ArrayList<PriceSlaughterLamb>();
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLambToAttach : priceSlaughterLambListNew) {
                priceSlaughterLambListNewPriceSlaughterLambToAttach = em.getReference(priceSlaughterLambListNewPriceSlaughterLambToAttach.getClass(), priceSlaughterLambListNewPriceSlaughterLambToAttach.getId());
                attachedPriceSlaughterLambListNew.add(priceSlaughterLambListNewPriceSlaughterLambToAttach);
            }
            priceSlaughterLambListNew = attachedPriceSlaughterLambListNew;
            priceSourceSlaughterHeader.setPriceSlaughterLambList(priceSlaughterLambListNew);
            List<PriceSlaughter> attachedPriceSlaughterListNew = new ArrayList<PriceSlaughter>();
            for (PriceSlaughter priceSlaughterListNewPriceSlaughterToAttach : priceSlaughterListNew) {
                priceSlaughterListNewPriceSlaughterToAttach = em.getReference(priceSlaughterListNewPriceSlaughterToAttach.getClass(), priceSlaughterListNewPriceSlaughterToAttach.getId());
                attachedPriceSlaughterListNew.add(priceSlaughterListNewPriceSlaughterToAttach);
            }
            priceSlaughterListNew = attachedPriceSlaughterListNew;
            priceSourceSlaughterHeader.setPriceSlaughterList(priceSlaughterListNew);
            List<SlaughterProperty> attachedSlaughterPropertyListNew = new ArrayList<SlaughterProperty>();
            for (SlaughterProperty slaughterPropertyListNewSlaughterPropertyToAttach : slaughterPropertyListNew) {
                slaughterPropertyListNewSlaughterPropertyToAttach = em.getReference(slaughterPropertyListNewSlaughterPropertyToAttach.getClass(), slaughterPropertyListNewSlaughterPropertyToAttach.getId());
                attachedSlaughterPropertyListNew.add(slaughterPropertyListNewSlaughterPropertyToAttach);
            }
            slaughterPropertyListNew = attachedSlaughterPropertyListNew;
            priceSourceSlaughterHeader.setSlaughterPropertyList(slaughterPropertyListNew);
            priceSourceSlaughterHeader = em.merge(priceSourceSlaughterHeader);
            for (PriceSlaughterLamb priceSlaughterLambListNewPriceSlaughterLamb : priceSlaughterLambListNew) {
                if (!priceSlaughterLambListOld.contains(priceSlaughterLambListNewPriceSlaughterLamb)) {
                    PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb = priceSlaughterLambListNewPriceSlaughterLamb.getPriceSourceSlaughterHeaderId();
                    priceSlaughterLambListNewPriceSlaughterLamb.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                    priceSlaughterLambListNewPriceSlaughterLamb = em.merge(priceSlaughterLambListNewPriceSlaughterLamb);
                    if (oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb != null && !oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb.equals(priceSourceSlaughterHeader)) {
                        oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb.getPriceSlaughterLambList().remove(priceSlaughterLambListNewPriceSlaughterLamb);
                        oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb = em.merge(oldPriceSourceSlaughterHeaderIdOfPriceSlaughterLambListNewPriceSlaughterLamb);
                    }
                }
            }
            for (PriceSlaughter priceSlaughterListNewPriceSlaughter : priceSlaughterListNew) {
                if (!priceSlaughterListOld.contains(priceSlaughterListNewPriceSlaughter)) {
                    PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter = priceSlaughterListNewPriceSlaughter.getPriceSourceSlaughterHeaderId();
                    priceSlaughterListNewPriceSlaughter.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                    priceSlaughterListNewPriceSlaughter = em.merge(priceSlaughterListNewPriceSlaughter);
                    if (oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter != null && !oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter.equals(priceSourceSlaughterHeader)) {
                        oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter.getPriceSlaughterList().remove(priceSlaughterListNewPriceSlaughter);
                        oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter = em.merge(oldPriceSourceSlaughterHeaderIdOfPriceSlaughterListNewPriceSlaughter);
                    }
                }
            }
            for (SlaughterProperty slaughterPropertyListNewSlaughterProperty : slaughterPropertyListNew) {
                if (!slaughterPropertyListOld.contains(slaughterPropertyListNewSlaughterProperty)) {
                    PriceSourceSlaughterHeader oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty = slaughterPropertyListNewSlaughterProperty.getPriceSourceSlaughterHeaderId();
                    slaughterPropertyListNewSlaughterProperty.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);
                    slaughterPropertyListNewSlaughterProperty = em.merge(slaughterPropertyListNewSlaughterProperty);
                    if (oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty != null && !oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty.equals(priceSourceSlaughterHeader)) {
                        oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty.getSlaughterPropertyList().remove(slaughterPropertyListNewSlaughterProperty);
                        oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty = em.merge(oldPriceSourceSlaughterHeaderIdOfSlaughterPropertyListNewSlaughterProperty);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = priceSourceSlaughterHeader.getId();
                if (findPriceSourceSlaughterHeader(id) == null) {
                    throw new NonexistentEntityException("The priceSourceSlaughterHeader with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PriceSourceSlaughterHeader priceSourceSlaughterHeader;
            try {
                priceSourceSlaughterHeader = em.getReference(PriceSourceSlaughterHeader.class, id);
                priceSourceSlaughterHeader.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The priceSourceSlaughterHeader with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<PriceSlaughterLamb> priceSlaughterLambListOrphanCheck = priceSourceSlaughterHeader.getPriceSlaughterLambList();
            for (PriceSlaughterLamb priceSlaughterLambListOrphanCheckPriceSlaughterLamb : priceSlaughterLambListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This PriceSourceSlaughterHeader (" + priceSourceSlaughterHeader + ") cannot be destroyed since the PriceSlaughterLamb " + priceSlaughterLambListOrphanCheckPriceSlaughterLamb + " in its priceSlaughterLambList field has a non-nullable priceSourceSlaughterHeaderId field.");
            }
            List<PriceSlaughter> priceSlaughterListOrphanCheck = priceSourceSlaughterHeader.getPriceSlaughterList();
            for (PriceSlaughter priceSlaughterListOrphanCheckPriceSlaughter : priceSlaughterListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This PriceSourceSlaughterHeader (" + priceSourceSlaughterHeader + ") cannot be destroyed since the PriceSlaughter " + priceSlaughterListOrphanCheckPriceSlaughter + " in its priceSlaughterList field has a non-nullable priceSourceSlaughterHeaderId field.");
            }
            List<SlaughterProperty> slaughterPropertyListOrphanCheck = priceSourceSlaughterHeader.getSlaughterPropertyList();
            for (SlaughterProperty slaughterPropertyListOrphanCheckSlaughterProperty : slaughterPropertyListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This PriceSourceSlaughterHeader (" + priceSourceSlaughterHeader + ") cannot be destroyed since the SlaughterProperty " + slaughterPropertyListOrphanCheckSlaughterProperty + " in its slaughterPropertyList field has a non-nullable priceSourceSlaughterHeaderId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(priceSourceSlaughterHeader);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PriceSourceSlaughterHeader> findPriceSourceSlaughterHeaderEntities() {
        return findPriceSourceSlaughterHeaderEntities(true, -1, -1);
    }

    public List<PriceSourceSlaughterHeader> findPriceSourceSlaughterHeaderEntities(int maxResults, int firstResult) {
        return findPriceSourceSlaughterHeaderEntities(false, maxResults, firstResult);
    }

    private List<PriceSourceSlaughterHeader> findPriceSourceSlaughterHeaderEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PriceSourceSlaughterHeader.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PriceSourceSlaughterHeader findPriceSourceSlaughterHeader(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PriceSourceSlaughterHeader.class, id);
        } finally {
            em.close();
        }
    }

    public int getPriceSourceSlaughterHeaderCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PriceSourceSlaughterHeader> rt = cq.from(PriceSourceSlaughterHeader.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
